﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TMaterialFoundationRepository : BaseRepository<TMaterial>, ITMaterialFoundationRepository
    {
        public TMaterialFoundationRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public bool AddBasicMaterial(out string msg ,FoundationMaterialDto dto)
        {
            msg = "";
            TMaterial material = new TMaterial();
            material.Code = dto.Code;
            material.Name = dto.Name;
            material.Remark = dto.Remark;
            material.ModelSpec = dto.ModelSpec;
            material.UnitName = dto.UnitName;
            material.UnitCode = dto.UnitCode;
            if(dto.TypeId == 0)
            {
                material.TypeName = " 原材料";
            }else if(dto.TypeId == 1)
            {
                material.TypeName = "半成品";
            }else if (dto.TypeId == 2)
            {
                material.TypeName = "成品";
            }
            else
            {
                msg = "传入的typeId不正确，请检查传参是否正确";
                return false;
            }

            material.MaterialGroupCode = dto.MaterialGroupCode;
            material.MaterialGroupName = dto.MaterialGroupName;
            material.TypeId = dto.TypeId;
            material.CreateDate = DateTime.Now;
            Context.TMaterial.Add(material);
            if(Context.SaveChanges() > 0)
            {
                return true;
            }
            return false;

        }
    }
}
